package snail.tiger.com.tigerplayer.tools;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.util.Log;

import java.io.IOException;

import snail.tiger.com.tigerplayer.R;

public class AudioPlayerUtil {
    private static AudioPlayerUtil audioPlayerUtil;

    /**
     * 适合播放声音短，文件小
     * 可以同时播放多种音频
     * 消耗资源较小
     */
    private SoundPool mSoundPool;

    /**
     * 不可以同时播放多种音频
     * 消耗资源较大
     */
    private MediaPlayer mMediaPlayer;

    public static AudioPlayerUtil getInstance() {
        if (audioPlayerUtil == null) {
            audioPlayerUtil = new AudioPlayerUtil();
        }
        return audioPlayerUtil;
    }

    private AudioPlayerUtil() {

    }

    public void init(Context context) {
        initSound(context);
        initMedia();
    }

    private void initSound(Context context) {
        mSoundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);

        // 1-12 播报声
        mSoundPool.load(context, R.raw.red_benz, 1);
        mSoundPool.load(context, R.raw.green_benz, 1);
        mSoundPool.load(context, R.raw.yellow_benz, 1);
        mSoundPool.load(context, R.raw.red_bmw, 1);
        mSoundPool.load(context, R.raw.green_bmw, 1);
        mSoundPool.load(context, R.raw.yellow_bmw, 1);
        mSoundPool.load(context, R.raw.red_audi, 1);
        mSoundPool.load(context, R.raw.green_audi, 1);
        mSoundPool.load(context, R.raw.yellow_audi, 1);
        mSoundPool.load(context, R.raw.red_jetta, 1);
        mSoundPool.load(context, R.raw.green_jetta, 1);
        mSoundPool.load(context, R.raw.yellow_jetta, 1);

        mSoundPool.load(context, R.raw.bets_view_music, 1);// 13 下注按钮
        mSoundPool.load(context, R.raw.tiger, 1);// 14 转盘

        mSoundPool.load(context, R.raw.refresh, 1);// 15 刷新
    }

    private void initMedia() {
        mMediaPlayer = new MediaPlayer();
    }

    /**
     * @param speed 语速（成倍）
     */
    public void playSound(int number, int speed) {
        Log.d("tag", "number " + number);
        playSound(number, 0, speed);
    }

    /**
     * @param speed  语速（成倍）
     * @param repeat 重复次数
     */
    public void playSound(int number, int repeat, int speed) {
        Log.d("tag", "number " + number);
        mSoundPool.play(number, 0.50f, 0.50f, 0, repeat, speed);
    }

    public void stopSound(int number) {
        Log.d("tag", "number " + number);
        mSoundPool.stop(number);
    }

    public void pauseSound(int number) {
        Log.d("tag", "number " + number);
        mSoundPool.pause(number);
    }

    public void resumeSound(int number) {
        Log.d("tag", "number " + number);
        mSoundPool.resume(number);
    }

    public void releaseSound() {
        mSoundPool.release();
    }
}
